package com.wd.cloud.applet.common.constant.dataverify;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式
 *
 * @author hjx
 */
public class RegularConstant {
    /**
     * 正则表达式校验
     *
     * @param str
     * @param regex
     * @return
     */
    public static boolean regexCheck(String str, String regex) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 验证手机号码格式正则表达式
     */
    public static final String REGEX_MOBILE = "^((13[0-9])|(14[5-9])|(15([0-3]|[5-9]))|(16[2|5|6|7])|(17[0-9])|(18[0-9])|(19[1|3|5|8|9]))\\d{8}$";

    /**
     * 验证固定电话号码格式正则表达式
     */
    public static final String REGEX_TELEPHONE = "^(0[0-9]{2,3})?([\\-,\\_]{1})?([1-9][0-9]{6,7})$";


    /**
     * 验证URL地址
     */
    public static final String REGEX_URL = "^([hH][tT]{2}[pP]:/*|[hH][tT]{2}[pP][sS]:/*|[fF][tT][pP]:/*)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+(\\?{0,1}(([A-Za-z0-9-~]+\\={0,1})([A-Za-z0-9-~]*)\\&{0,1})*)$";


    /**
     * 校验车牌号码
     */
    public static final String REGEX_LICENCE_PLATE = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5})|([A-HJ-Z]{1}(([DF]{1}[A-HJ-NP-Z0-9]{1}[0-9]{4})|([0-9]{5}[DF]{1})))|([A-HJ-Z]{1}[A-D0-9]{1}[0-9]{3}警)))|([0-9]{6}使)|((([沪粤川云桂鄂陕蒙藏黑辽渝]{1}A)|鲁B|闽D|蒙E|蒙H)[0-9]{4}领)|(WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼·•]{1}[0-9]{4}[TDSHBXJ0-9]{1})|([VKHBSLJNGCE]{1}[A-DJ-PR-TVY]{1}[0-9]{5})";

    /**
     * 密码策略
     */
    public static final String PASSWORD_POLICY = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,32}$";

    /**
     * 用户名策略
     * 4到16位（字母，数字，下滑线，减号）
     */
    public static final String REGEX_ACCOUNT = "^[a-zA-Z]{1}([a-zA-Z0-9]|[._-]){3,15}$";

    /**
     * 校验真实姓名
     */
    public static final String REGEX_REAL_NAME = "^[\\u4e00-\\u9fa5]{2,4}$";

    /**
     * 校验18位营业执照
     */
    public static final String REGEX_LICENSE = "^(?:(?![IOZSV])[\\dA-Z]){2}\\d{6}(?:(?![IOZSV])[\\dA-Z]){10}$";


    /**
     * 营业执照 统一社会信用代码（15位）
     *
     * @param license
     * @return
     */
    public static boolean isLicense15(String license) {
        if ("".equals(license) || " ".equals(license)) {
            return false;
        } else if (license.length() != 15) {
            return false;
        } else {
            return isBusinessLicense(license);
        }
    }

    /**
     * 获取 营业执照注册号的校验码
     *
     * @param businessLicense 为15为返回1为有效，否则无效；传入14为则会计算出第15位的校验码。
     * @return
     */
    private static int getCheckCode(String businessLicense, boolean getCheckCode) {
        int result = -1;
        if (null == businessLicense || "".equals(businessLicense.trim()) || businessLicense.length() != 15) {
            return result;
        } else {
            int ti = 0;
            int si = 0;
            int cj = 0;
            int pj = 10;
            for (int i = 0; i < businessLicense.length(); i++) {
                ti = Integer.parseInt(businessLicense.substring(i, i + 1));
                si = pj + ti;
                cj = (0 == si % 10 ? 10 : si % 10) * 2;
                pj = (cj % 11) == 0 ? 10 : (cj % 11);
                if (i == businessLicense.length() - 2 && getCheckCode) {
                    result = (1 - pj < 0 ? 11 - pj : 1 - pj) % 10;
                    return result;
                }
                if (i == businessLicense.length() - 1) {
                    result = si % 10;
                }
            }
        }
        return result;
    }

    private static boolean isBusinessLicense(String businessLicense) {
        return 1 == getCheckCode(businessLicense, false);
    }

    /**
     * 获取校验码
     *
     * @param businessLicense 15位的营业执照号码
     * @return
     */
    public static int getComputeCheckCode(String businessLicense) {
        return getCheckCode(businessLicense, true);
    }

}
